package com.lishe.lishe.lishe.module.excitation.presenter;

import com.lishe.base.mvp.presenter.BaseMvpPresenter;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.excitation.bean.GiveLikeBannerResponse;
import com.lishe.lishe.lishe.module.excitation.bean.GiveLikeMessageResponse;
import com.lishe.lishe.lishe.module.excitation.bean.GiveLikeWallListResponse;
import com.lishe.lishe.lishe.utils.LogUtil;
import com.lishe.lishe.lishe.utils.StringUtils;
import com.lishe.lishe.net.BaseProtocal;
import com.lishe.lishe.net.ResultListener;

import java.util.HashMap;
import java.util.Map;

/**
 * 标    题：
 * 功能描述：
 * 作    者：zouxx
 * 创建时间：2018/8/17
 */

public class GiveLikePresenter extends BaseMvpPresenter<GiveLikeWallView> {

    private GiveLikeWallModule module;

    public GiveLikePresenter() {
        module = new GiveLikeWallModule();
    }

    public void getBannerList(String token) {
        if (getMvpView() != null)
            getMvpView().onLoading("");
        module.getBannerList(token, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().getGiveLikeBanner((GiveLikeBannerResponse) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().onFailure(0, errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
            }
        });
    }

    public void getMessageList(String token) {
        if (getMvpView() != null)
            getMvpView().onLoading("");
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", token);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        module.getMessageList(token, sign, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().getGiveLikeMessage((GiveLikeMessageResponse) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().onFailure(2, errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
            }
        });
    }

    public void getGiveLikeList(int page, int pageSize) {
        if (getMvpView() != null)
            getMvpView().onLoading("");
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", Globals.getToken());
        signParams.put("pageNo", String.valueOf(page));
        signParams.put("pageSize", String.valueOf(pageSize));
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        module.getGiveLikeList(Globals.getToken(), page, pageSize, sign, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().getGiveLikeList((GiveLikeWallListResponse) mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().onFailure(1, errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
            }
        });
    }

    public void cancelHitEvent(String eventId) {
        if (getMvpView() != null)
            getMvpView().onLoading("");
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", Globals.getToken());
        signParams.put("eventId", eventId);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        module.cancelHitEvent(Globals.getToken(), eventId, sign, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().cancelHitEvent(mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().onFailure(0, errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
            }
        });
    }

    public void hitEvent(String eventId) {
        if (getMvpView() != null)
            getMvpView().onLoading("");
        Map<String, String> signParams = new HashMap<>();
        signParams.put("token", Globals.getToken());
        signParams.put("eventId", eventId);
        String sign = StringUtils.createSignString(signParams) + Globals.key;
        sign = StringUtils.md5(sign);
        module.hitEvent(Globals.getToken(), eventId, sign, new ResultListener() {
            @Override
            public void onSuccess(BaseProtocal mBaseProtocal) {
                if (getMvpView() != null)
                    getMvpView().hitEvent(mBaseProtocal);
            }

            @Override
            public void onFail(int errcode, String msg) {
                if (getMvpView() != null)
                    getMvpView().onFailure(0, errcode, msg);
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(e.getMessage());
            }
        });
    }
}
